package fem2.jlinalg;

import fem2.NotImplementedError;
import inf.jlinalg.IMatrix;
import inf.jlinalg.IMatrixReadOnly;

public class TriagonalMatrix implements IMatrix {

	private static final long serialVersionUID = 1L;

	private double[] diag;
	private double[] upper;
	private double[] lower;
	private int n;

	public TriagonalMatrix(int n) {
		this.n = n;
		diag = new double[n];
		upper = new double[n - 1];
		lower = new double[n - 1];
	}

	@Override
	public double get(int i, int j) {
		if (i == j - 1) {
			return upper[i];
		} else if (i == j + 1) {
			return lower[j];
		} else if (i == j) {
			return diag[i];
		} else {
			// assert i < n : "Illegal row index: " + i + ">=" + n;
			// assert j < n : "Illegal column index: " + j + ">=" + n;
			return 0;
		}
	}

	@Override
	public int getColumnCount() {
		return n;
	}

	@Override
	public int getRowCount() {
		return n;
	}

	@Override
	public void add(int i, int j, double value) {
		if (i == j - 1) {
			upper[i] += value;
		} else if (i == j + 1) {
			lower[j] += value;
		} else if (i == j) {
			diag[i] += value;
		} else {
			// assert i < n : "Illegal row index: " + i + ">=" + n;
			// assert j < n : "Illegal column index: " + j + ">=" + n;
		}
	}

	@Override
	public void set(int i, int j, double value) {
		if (i == j - 1) {
			upper[i] = value;
		} else if (i == j + 1) {
			lower[j] = value;
		} else if (i == j) {
			diag[i] = value;
		} else {
			// assert i < n : "Illegal row index: " + i + ">=" + n;
			// assert j < n : "Illegal column index: " + j + ">=" + n;
		}
	}

	@Override
	public void assign(IMatrixReadOnly a) {
		/*
		 * TODO
		 */
		throw new NotImplementedError();
	}

	@Override
	public Object clone() {
		TriagonalMatrix m = new TriagonalMatrix(n);
		System.arraycopy(upper, 0, m.upper, 0, n - 1);
		System.arraycopy(lower, 0, m.lower, 0, n - 1);
		System.arraycopy(diag, 0, m.diag, 0, n);
		return m;
	}
}
